import storeHelper from "@/utils/TauriStore";
import { normalRules } from '@/common/nomalRules';
type BanPick = 'ban' | 'pick';
type Excutor = 'hunter' | 'survivor';
type OptionsKey = 'surHalf' | 'hunHalf';
type WaitCharAnimation = 'sur1' | 'sur2' | 'sur3' | 'sur4' | 'hun'
type ComponentType = 'select' | 'talent-sur' | 'talent-hun'; //下拉款和组件 
// 流程接口
export interface ProcessInterface {
    id: string
    title: string
    task: BanPick
    excuteNumber: number
    bindKey: string[],
    excutor: Excutor
    excutorTeam: string
    taskSeniar: number,
    optionsKey: OptionsKey,
    placeholder: string[],
    isInGlobalBan: boolean,
    showWaitAnimation?: WaitCharAnimation[],
    componentType: ComponentType
}

// 配置规则接口
export interface OptionRuleConfig {
    id: string,
    title: string,
    task: BanPick,
    excuteNumber: number,
    excutor: Excutor,
    bindKey: string[],
    taskSeniar: number,
    optionsKey: OptionsKey,
    isInGlobalBan: boolean,
    showWaitAnimation?: WaitCharAnimation[],
    componentType: ComponentType
}

// 生成流程函数新版
export function generateProcessObject(session: string, gameInfo: GameInfo, teamInfo: Partial<TeamInfo>) {
    return new Promise<ProcessInterface[]>(async (resolve, reject) => {
        let ruleConfig = null;
        const USE_RULE_ID = await storeHelper.get('use_rule'); //获取现在使用的规则
        //如果没有
        if (USE_RULE_ID === null) {
            ruleConfig = normalRules;
        } else {
            const rule = await storeHelper.get(`ruleOptions_${USE_RULE_ID}`);
            ruleConfig = rule;
        }
        if (!ruleConfig || typeof ruleConfig !== 'object') {
            reject(new Error('规则配置错误，请检查！'));
            return;
        }
        const ruleProcess = ruleConfig[session]; //取出该回合的规则
        const surTeam = gameInfo.survivorTeam;
        const hunTeam = gameInfo.hunterTeam;
        const surMember: string[] = teamInfo[surTeam]?.survivor ?? [];
        const hunMember: string = teamInfo[hunTeam]?.hunter ?? '';
        const surTeamName = teamInfo[surTeam]?.teamName ?? 'Survivor Team';
        const hunTeamName = teamInfo[hunTeam]?.teamName ?? 'Hunter Team';
        let surIndex = 0;
        for (let i = 0; i < ruleProcess.length; i++) {
            const step = ruleProcess[i];
            step.placeholder = [];
            if (step.excutor === 'survivor') {
                if (step.task === 'pick') {
                    if (step.bindKey && Array.isArray(step.bindKey)) {
                        for (let j = 0; j < step.bindKey.length; j++) {
                            step.placeholder[j] = `${surMember[surIndex]}选取求生者${j + 1}`;
                            if (surIndex < surMember.length - 1) {
                                surIndex++;
                            }
                        }
                    }
                } else {
                    if (step.bindKey && Array.isArray(step.bindKey)) {
                        for (let j = 0; j < step.bindKey.length; j++) {
                            step.placeholder[j] = '求生者禁用监管者';
                        }
                    }
                }
            } else {
                if (step.task === 'pick') {
                    step.placeholder[0] = `${hunMember}选取监管者`;
                } else {
                    if (step.bindKey && Array.isArray(step.bindKey)) {
                        for (let j = 0; j < step.bindKey.length; j++) {
                            step.placeholder[j] = `${hunMember}禁用求生者`;
                        }
                    }
                }
            }
            step.excutorTeam = step.excutor === 'survivor' ? surTeamName : hunTeamName; //设置执行队伍
        }
        resolve(ruleProcess);
    })
}

// 获取tab流程
export function getTabProcess() {
    return new Promise<string[]>(async (resolve, reject) => {
        let ruleConfig = null;
        const ruleProcess: string[] = [];
        const USE_RULE_ID = await storeHelper.get('use_rule'); //获取现在使用的规则
        //如果没有
        if (USE_RULE_ID === null) {
            ruleConfig = normalRules;
        } else {
            const rule = await storeHelper.get(`ruleOptions_${USE_RULE_ID}`);
            ruleConfig = rule;
        }
        if (!ruleConfig || typeof ruleConfig !== 'object') {
            reject(new Error('规则配置错误，请检查！'));
            return;
        }
        for (const key in ruleConfig) {
            if (Object.prototype.hasOwnProperty.call(ruleConfig, key)) {
                ruleProcess.push(key)
            }
        }
        resolve(ruleProcess);
    })
}

